package com.twu.biblioteca.models;


import com.twu.biblioteca.helpers.Encryption;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UserDAO {

    private static List<User> users = new ArrayList<User>();
    private static User loggedUser;

    public static final void init(){
        users.add(new User("000-00000", new Encryption().encodeSHA("java"), "Carlos", "jcsouza@thoughtworks.com", "95873448"));
        users.add(new User("111-11111", new Encryption().encodeSHA("tdd"), "Carlos", "jcsouza@thoughtworks.com", "95873448"));
        users.add(new User("222-22222", new Encryption().encodeSHA("agile"), "Carlos", "jcsouza@thoughtworks.com", "95873448"));
    }

    public static final boolean log(User user){
        for (User userSystem : users){
            if(userSystem.equals(user)){
                loggedUser = user;

                return true;
            }
        }

        return false;
    }

    public static final boolean checkPassword(byte[] password){
        if(loggedUser instanceof User){
            return Arrays.equals(password, loggedUser.getPassword());
        }

        return false;
    }

    public static final User getLoggedUser(){
        return loggedUser;
    }
}
